package com.example.day07_homework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ListView listView;
	private List<String> items;
	private MyAdapter adapter;
	private Map<Integer,Boolean> isCheckedMap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
		adapter=new MyAdapter();
	    listView.setAdapter(adapter);
	}

	
	public void initView(){
    	items=new ArrayList<String>();
    	isCheckedMap=new HashMap<Integer, Boolean>();
    	listView=(ListView)findViewById(R.id.lv);
    	for(int i=0;i<20;i++){
    		items.add("item"+i);
    		isCheckedMap.put(i, false);
    	}
    }
	public void selectAll(View view){
		for(int i=0;i<isCheckedMap.size();i++){
			isCheckedMap.put(i, true);
		}
		adapter.notifyDataSetChanged();
	}
	public void selectAnti(View view){
		for(int i=0;i<isCheckedMap.size();i++){
			if(isCheckedMap.get(i)){
				isCheckedMap.put(i, false);
			}else{
				isCheckedMap.put(i, true);
			}
		}
		adapter.notifyDataSetChanged();
	}
	public void selectNo(View view){
		for(int i=0;i<isCheckedMap.size();i++){
			isCheckedMap.put(i, false);
		}
		adapter.notifyDataSetChanged();
	}
	public void submit(View view){
		for(int i=0;i<isCheckedMap.size();i++){
			if(isCheckedMap.get(i)){
				Log.d("1507", "position:"+i);
			}
		}
		adapter.notifyDataSetChanged();
	}

	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder=null;
			final int index=position;
			if(convertView==null){
				convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item, null);
				holder=new ViewHolder();
				holder.tv=(TextView)convertView.findViewById(R.id.item_tv);
				holder.chk=(CheckBox)convertView.findViewById(R.id.chkbox);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder)convertView.getTag();
			}
			holder.tv.setText(items.get(position));
			holder.chk.setChecked(isCheckedMap.get(position));
			holder.chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if (isChecked) {
						isCheckedMap.put(index, true);
					}else{
						isCheckedMap.put(index, false);
					}
				}
			});
			return convertView;
		}
		
	}
	final class ViewHolder{
		TextView tv;
		CheckBox chk;
	}
	
}
